package com.example.aopdemo;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class SecurityAspect {

    // 5. 权限检查（仅对add和delete方法）
    @Before("execution(* com.example.aopdemo.OrderService.add(..)) || " +
            "execution(* com.example.aopdemo.OrderService.delete(..))")
    public void checkPermission(JoinPoint joinPoint) {
        // 模拟权限检查（0有权限，1无权限）
        int permission = (int) (Math.random() * 2);

        if (permission == 0) {
            System.out.println("权限检查通过，允许执行 " + joinPoint.getSignature().getName());
        } else {
            System.out.println("权限检查失败，不允许执行 " + joinPoint.getSignature().getName());
            throw new SecurityException("权限不足，无法执行操作");
        }
    }
}